package com.pearadmin.modules.sys.controller.work;

import com.github.pagehelper.PageInfo;
import com.pearadmin.common.constant.ControllerConstant;
import com.pearadmin.common.tools.SecurityUtil;
import com.pearadmin.common.tools.SequenceUtil;
import com.pearadmin.common.tools.string.Convert;
import com.pearadmin.common.web.base.BaseController;
import com.pearadmin.common.web.domain.request.PageDomain;
import com.pearadmin.common.web.domain.response.Result;
import com.pearadmin.common.web.domain.response.module.ResultTable;
import com.pearadmin.modules.sys.domain.SysUser;
import com.pearadmin.modules.sys.domain.WorkCasting;
import com.pearadmin.modules.sys.service.ISysDeptService;
import com.pearadmin.modules.sys.service.IWorkCastingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;

/**
 * 浇筑工作记录Controller
 *
 * @author Mr.Ling
 * @date 2021-11-26
 */
@RestController
@Api(tags = {"浇筑工作记录"})
@RequestMapping(ControllerConstant.API_WORK_PREFIX + "casting")
public class WorkCastingController extends BaseController {
    private String prefix = "work/casting" ;

    @Autowired
    private IWorkCastingService workCastingService;
    @Autowired
    private ISysDeptService sysDeptService;
    @GetMapping("/main")
    @PreAuthorize("hasPermission('/work/casting/main','work:casting:main')")
    public ModelAndView main(Model model) {
        model.addAttribute("depts", sysDeptService.list(null));
        return jumpPage(prefix + "/main");
    }

    /**
     * 查询浇筑工作记录列表
     */
    @ResponseBody
    @ApiOperation("浇筑工作记录列表")
    @GetMapping("/data")
    @PreAuthorize("hasPermission('/work/casting/data','work:casting:data')")
    public ResultTable list(@ModelAttribute WorkCasting workCasting, PageDomain pageDomain) {
        PageInfo<WorkCasting> pageInfo = workCastingService.selectWorkCastingPage(workCasting, pageDomain);
        return pageTable(pageInfo.getList(), pageInfo.getTotal());
    }

    /**
     * 新增浇筑工作记录
     */
    @GetMapping("/add")
    @PreAuthorize("hasPermission('/work/casting/add','work:casting:add')")
    public ModelAndView add(Model model) {
        SysUser sysUser = (SysUser) SecurityUtil.currentUserObj();
        model.addAttribute("dept", sysDeptService.getById(sysUser.getDeptId()));
        return jumpPage(prefix + "/add");
    }

    /**
     * 新增保存浇筑工作记录
     */
    @ResponseBody
    @ApiOperation("浇筑工作记录保存")
    @PostMapping("/save")
    @PreAuthorize("hasPermission('/work/casting/add','work:casting:add')")
    public Result save(@RequestBody WorkCasting workCasting) {
        SysUser sysUser = (SysUser) SecurityUtil.currentUserObj();
        workCasting.setCreateTime(LocalDateTime.now());
        workCasting.setCreateBy(sysUser.getUserId());
        workCasting.setCreateName(sysUser.getRealName());
        workCasting.setId(SequenceUtil.makeStringId());
        return decide(workCastingService.insertWorkCasting(workCasting));
    }

    /**
     * 修改浇筑工作记录
     */
    @GetMapping("/edit")
    @PreAuthorize("hasPermission('/work/casting/edit','work:casting:edit')")
    public ModelAndView edit(String id, ModelMap mmap) {
        WorkCasting workCasting = workCastingService.selectWorkCastingById(id);
        workCasting.setUpdateTime(LocalDateTime.now());
        mmap.put("workCasting", workCasting);
        return jumpPage(prefix + "/edit");
    }

    /**
     * 修改保存浇筑工作记录
     */
    @ResponseBody
    @ApiOperation("浇筑工作记录更新")
    @PutMapping("/update")
    @PreAuthorize("hasPermission('/work/casting/edit','work:casting:edit')")
    public Result update(@RequestBody WorkCasting workCasting) {
        SysUser sysUser = (SysUser) SecurityUtil.currentUserObj();
        workCasting.setUpdateTime(LocalDateTime.now());
        workCasting.setUpdateBy(sysUser.getUserId());
        workCasting.setUpdateName(sysUser.getUsername());
        return decide(workCastingService.updateWorkCasting(workCasting));
    }

    /**
     * 删除浇筑工作记录
     */
    @ResponseBody
    @ApiOperation("浇筑工作记录批量删除")
    @DeleteMapping("/batchRemove")
    @PreAuthorize("hasPermission('/work/casting/remove','work:casting:remove')")
    public Result batchRemove(String ids) {
        return decide(workCastingService.deleteWorkCastingByIds(Convert.toStrArray(ids)));
    }

    /**
     * 删除
     */
    @ResponseBody
    @ApiOperation("根据浇筑工作记录Id 删除")
    @DeleteMapping("/remove/{id}")
    @PreAuthorize("hasPermission('/work/casting/remove','work:casting:remove')")
    public Result remove(@PathVariable("id") String id) {
        return decide(workCastingService.deleteWorkCastingById(id));
    }
}
